package com.youdianda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youdianda.dto.YddAdvposPageDTO;
import com.youdianda.entity.YddAdvimg;
import com.youdianda.entity.YddAdvpos;
import com.youdianda.mapper.YddAdvimgMapper;
import com.youdianda.result.PageResult;
import com.youdianda.result.Result;
import com.youdianda.service.YddAdvimgService;
import com.youdianda.service.YddAdvposService;
import com.youdianda.mapper.YddAdvposMapper;
import com.youdianda.vo.YddAdvimgVO;
import com.youdianda.vo.YddAdvposVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
* @author xliu
* @description 针对表【ydd_advpos】的数据库操作Service实现
* @createDate 2023-05-14 16:01:34
*/
@Service
public class YddAdvposServiceImpl extends ServiceImpl<YddAdvposMapper, YddAdvpos>
    implements YddAdvposService{
    @Autowired
    private YddAdvposService yddAdvposService;
    @Autowired
    private YddAdvimgService yddAdvimgService;
    @Autowired
    private  YddAdvposMapper yddAdvposMapper;
    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;
    @Override
    public Result list(YddAdvposPageDTO yddAdvposPageDTO) {
        /*分页查询*/
        IPage<YddAdvpos> page = new Page<>(Integer.valueOf(yddAdvposPageDTO.getPage()),Integer.valueOf(yddAdvposPageDTO.getLimit()));
        LambdaQueryWrapper<YddAdvpos> wrapper = new LambdaQueryWrapper<>();
        /*模糊查询*/
        if (yddAdvposPageDTO.getAdvposdesc() != null) {
            wrapper.like(YddAdvpos::getAdvposdesc, yddAdvposPageDTO.getAdvposdesc());
        }
        if (yddAdvposPageDTO.getAdvposname() != null) {
            wrapper.like(YddAdvpos::getAdvposname, yddAdvposPageDTO.getAdvposname());
        }


        /*封装结果*/
        IPage<YddAdvpos> page1 = yddAdvposMapper.selectPage(page, wrapper);
        List<YddAdvposVO> list = new ArrayList<>();
        for (YddAdvpos record : page1.getRecords()) {
            YddAdvposVO yddAdvposVO = new YddAdvposVO();
            LambdaQueryWrapper<YddAdvimg> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(YddAdvimg::getAdvimgpos,record.getId());
            long count = yddAdvimgService.count(wrapper1);
            BeanUtils.copyProperties(record,yddAdvposVO);
            yddAdvposVO.setAdvposnum(count+"");
            list.add(yddAdvposVO);
        }
        Integer total = Math.toIntExact(page1.getTotal());
        Integer pages = Math.toIntExact(page1.getPages());
        Integer size = Math.toIntExact(page1.getSize());
        Integer current = Math.toIntExact(page1.getCurrent());
       // List<YddAdvpos> records = page1.getRecords();
        PageResult pageResult = new PageResult(total,pages,size,current,list);
        return Result.success(pageResult);



    }
             /*广告位修改*/
    @Transactional
    @Override
    public Result update(YddAdvpos yddAdvpos) {
        YddAdvpos yddAdvpos1 = new YddAdvpos();
        BeanUtils.copyProperties(yddAdvpos,yddAdvpos1);
        yddAdvposService.updateById(yddAdvpos1);
        return Result.success();
    }

    @Transactional
   @Override
    public Result<Object> del(Integer id) {
        this.removeById(id);
        return Result.success();
    }




    @Override
    public Result rowInfo(Integer id) {
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        return Result.success(yddAdvpos);
    }

    @Override
    public Result add(YddAdvpos yddAdvpos) {
        boolean save = save(yddAdvpos);
        return Result.success(save);

    }

}




